package com.example.aidldemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.aidlsdk.ICalculator;
import com.example.aidlsdk.bean.Sample;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    private ICalculator mCalculator;
    private final ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mCalculator = ICalculator.Stub.asInterface(service);
            try {
                int a = mCalculator.add(1,2);
                Sample sample =new Sample();
                sample.setNum(16);
                mCalculator.optionParcel(sample);
                Log.d(TAG, "onServiceConnected: "+a);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: ");
        bindToService();
    }

    private void bindToService(){
        Intent intent = new Intent();
        intent.setAction("com.aidldemo.CALCULATOR_SERVICE");
        intent.setComponent(new ComponentName("com.example.aidldemoservice","com.example.aidldemoservice.CalculatorService"));
        boolean connected = bindService(intent,mServiceConnection,BIND_AUTO_CREATE);
        Log.d(TAG, "bindToService: connected is "+connected);
    }
}